package com.whlilong.jxc.filemanager.editor;

import java.util.List;

import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeColumn;

import com.whlilong.jxc.editor.support.DepartInfoEditorSupport;
import com.whlilong.jxc.listener.IEditorListener;
import com.whlilong.jxc.print.IconSource;
import com.whlilong.jxc.print.PDocument;
import com.whlilong.jxc.print.PHLine;
import com.whlilong.jxc.print.PTableBoxProvider;
import com.whlilong.jxc.print.PTextBox;
import com.whlilong.jxc.print.PVSpace;
import com.whlilong.jxc.print.PrintPreview;
import com.whlilong.jxc.print.SWTPTable;
import com.whlilong.jxc.service.editor.IEditorService;
import com.whlilong.jxc.sys.ExtentionInfo;
import com.whlilong.jxc.sys.SetTableColColorListener;
import com.whlilong.jxc.treemodel.BrandInfoTreeChildren;
import com.whlilong.jxc.treemodel.BrandInfoTreeParent;
import com.whlilong.jxc.ui.provider.BrandInfoContentProvider;
import com.whlilong.jxc.ui.provider.BrandInfoLabelProvider;
import com.whlilong.jxc.ui.provider.DepartInfoTreeContentProvider;
import com.whlilong.jxc.ui.provider.DepartInfoTreeLabelProvider;
import com.whlilong.jxc.ui.sorter.BrandInfoSorter;

/**
 * 辅助信息编辑器
 * 
 * @author sjy
 * 
 */
public class AidInfoEditor extends AbstractEditor implements IEditorService ,IEditorListener{

	private Table table;
	protected ExtentionInfo extentionInfo = null;
	private Label infoLabel;
	private Label departNoLabel;
	private Text departNoText;
	private Label departNameLabel;
	private Text departNameText;
	private Button findBt;
	protected String departId;
	protected String departName;

	public AidInfoEditor() {
		// TODO Auto-generated constructor stub
	}

	public Control createGroupText(Composite parent) {

		Composite container = new Composite(parent, SWT.NONE);
		final GridLayout gl = new GridLayout(2, false);
		container.setLayout(gl);
		createTreeViewer(container);
		createTableViewer(container);
		return container;
	}

	private void createTreeViewer(Composite parent) {
		// TODO Auto-generated method stub
		treeViewer = new TreeViewer(parent, SWT.BORDER | SWT.V_SCROLL
				| SWT.FULL_SELECTION);
		treeViewer.setUseHashlookup(true);

		GridData gd = new GridData();
		gd.verticalAlignment = SWT.TOP;
		final Tree tree = treeViewer.getTree();
		tree.setLayoutData(new GridData(GridData.FILL_VERTICAL));
		final TreeColumn category = new TreeColumn(tree, SWT.LEFT);
		category.setText("所有部门");
		category.setWidth(100);

		treeViewer.setContentProvider(new DepartInfoTreeContentProvider());
		treeViewer.setLabelProvider(new DepartInfoTreeLabelProvider());
		List list = extentionInfo.getInstance().getDepartTreeInfo();
		treeViewer.setInput(list);

		tree.setHeaderVisible(true);
		tree.setLinesVisible(true);

		treeViewer.expandToLevel(2);

		treeViewer.addSelectionChangedListener(new ISelectionChangedListener() {

			@Override
			public void selectionChanged(SelectionChangedEvent event) {
				// TODO Auto-generated method stub
				IStructuredSelection selection = (IStructuredSelection) event
						.getSelection();
				if (selection.getFirstElement() instanceof BrandInfoTreeChildren) {
					BrandInfoTreeChildren ktc = (BrandInfoTreeChildren) selection
							.getFirstElement();
					String id = ktc.getGoodsbrand().getGoodsbrandid();
					tableViewer.getTable().clearAll();
					// tableViewer.setInput(getGoodbrandinfo(id, null));
				}
				if (selection.getFirstElement() instanceof BrandInfoTreeParent) {
					// initial();
					tableViewer.getTable().clearAll();
					// tableViewer.setInput(list);
				}

			}
		});

	}

	private void createTableViewer(Composite parent) {
		// TODO Auto-generated method stub
		Composite composite = new Composite(parent, SWT.NONE);
		GridLayout gl = new GridLayout(4, false);
		composite.setLayout(gl);
		composite.setLayoutData(new GridData(GridData.FILL_BOTH));

		infoLabel = new Label(composite, SWT.NONE);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.heightHint = 20;
		gd.horizontalSpan = 4;
		infoLabel.setLayoutData(gd);

		gd = new GridData();
		gd.widthHint = 100;
		gd.heightHint = 20;
		departNoLabel = new Label(composite, SWT.RIGHT);
		departNoLabel.setText("部门编号：");
		departNoLabel.setLayoutData(gd);
		departNoText = new Text(composite, SWT.BORDER);
		departNoText.setLayoutData(gd);

		departNameLabel = new Label(composite, SWT.RIGHT);
		departNameLabel.setText("部门名称：");
		departNameLabel.setLayoutData(gd);
		departNameText = new Text(composite, SWT.BORDER);
		departNameText.setLayoutData(gd);

		findBt = new Button(composite, SWT.NONE);
		findBt.setText("查询F7");
		findBt.setLayoutData(gd);
		findBt.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseDown(MouseEvent e) {
				// TODO Auto-generated method stub
				departId = departNoText.getText();
				departName = departNameText.getText();
				// find(departId, departName);
			}

		});

		tableViewer = new TableViewer(composite, SWT.BORDER | SWT.H_SCROLL
				| SWT.FULL_SELECTION);
		tableViewer.setUseHashlookup(true);

		gd = new GridData(GridData.FILL_BOTH);
		gd.horizontalSpan = 4;
		table = tableViewer.getTable();
		table.setLayoutData(gd);
		table.setToolTipText("点击单元格对数据进行修改！");
		// 设置行高
		table.addListener(SWT.MeasureItem, new Listener() {
			public void handleEvent(Event event) {
				event.height = 20;
			}
		});

		DepartInfoEditorSupport editingSupport = new DepartInfoEditorSupport(
				tableViewer, 1, this);
		createColumn("ID", BrandInfoSorter.ID_ASC, BrandInfoSorter.ID_DESC,
				editingSupport);

		editingSupport = new DepartInfoEditorSupport(tableViewer, 2, this);
		createColumn("品牌编号", BrandInfoSorter.sbid_asc,
				BrandInfoSorter.sbid_desc, editingSupport);
		manager.addITextKeyChangeListener(editingSupport);
		editingSupport = new DepartInfoEditorSupport(tableViewer, 3, this);
		createColumn("商品品牌", BrandInfoSorter.NAME_ASC,
				BrandInfoSorter.NAME_DESC, editingSupport);
		manager.addITextKeyChangeListener(editingSupport);

		table.setHeaderVisible(true);
		table.setLinesVisible(true);

		// initial the tabelViewer
		tableViewer.setContentProvider(new BrandInfoContentProvider());
		tableViewer.setLabelProvider(new BrandInfoLabelProvider());
		// initial();
		// tableViewer.setInput(list);
		table.addListener(SWT.EraseItem, new SetTableColColorListener(table));

		table.select(0);
		table.setFocus();

	}

	@Override
	public void create() {
		// TODO Auto-generated method stub

	}

	@Override
	public void delete() {
		// TODO Auto-generated method stub

	}

	@Override
	public void print() {
		// TODO Auto-generated method stub
		final PDocument doc = new PDocument("print");

		PTextBox t;

		t = new PTextBox(doc);

		t.setText("武汉立隆客商信息");

		new PVSpace(doc, 0.5);
		new PHLine(doc, 0.02, SWT.COLOR_RED);
		new PVSpace(doc, 0.5);

		// create the table
		SWTPTable swttable = new SWTPTable(doc);
		swttable.setTable(table);
		swttable.setBoxProvider(new PTableBoxProvider());
		
		PrintPreview pr = new PrintPreview(null, "信息打印", IconSource
				.getImage("print"), doc);
		pr.onPrint();
	}

	@Override
	public void refresh() {
		// TODO Auto-generated method stub

	}

	@Override
	public void save() {
		// TODO Auto-generated method stub

	}

	@Override
	public void search() {
		// TODO Auto-generated method stub

	}

	@Override
	public void searchByCondition() {
		// TODO Auto-generated method stub

	}

	@Override
	public void addInfo() {
		// TODO Auto-generated method stub

	}

	@Override
	public List getList() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void noticeFault(String message) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void noticeSuccess() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void preprint() {
		// TODO Auto-generated method stub
		final PDocument doc = new PDocument("print");

		PTextBox t;

		t = new PTextBox(doc);

		t.setText("武汉立隆客商信息");

		new PVSpace(doc, 0.5);
		new PHLine(doc, 0.02, SWT.COLOR_RED);
		new PVSpace(doc, 0.5);

		// create the table
		SWTPTable swttable = new SWTPTable(doc);
		swttable.setTable(table);
		swttable.setBoxProvider(new PTableBoxProvider());
		
		PrintPreview pr = new PrintPreview(null, "信息打印", IconSource
				.getImage("print"), doc);
		pr.open();
	}

}
